<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel="stylesheet" type="text/css" href="${path}/css/system/upload.css">
<script type="text/javascript" src="${path}/js/system/account.js"></script>
<script type="text/javascript">
$.ajaxSetup({
    async : false  
}); 
$.canDelete=true;
	var dataGrid;
	$(function() {
		loadResidence('');
		var isAdmin = ${isAdmin};
		if(!isAdmin){
			$("#residenceId").combobox("select","${residenceId}");
			$("#residenceId").combobox("disable");
		}
		dataGrid=$("#dataGrid").datagrid({
			url:"/accountDoor/dataGrid?status=1&accountId=${account.id}",
			width:'100%',
			nowrap: false,
			striped: true,
			fit:true,
			collapsible:true,
			pageNumber:1,
			pageSize:10,
			pageList:[10],
			fitColumns:true,
			columns:[[
				{field:'residenceName',title:'小区',width:100},
				{field:'buildingName',title:'楼栋',width:50},
				{field:'unitName',title:'单元',width:50},
				{field:'doorName',title:'门牌',width:50},
				{field:'owner',title:'是否业主',width:80,formatter:function(value, row, index){
					return value==1?"是":"否";
				}},
				{field:'gmtCreate',title:'添加时间',width:100,sortable:true,formatter : function(value, row, index) {
					var unixTimestamp = new Date(value);  
                    return unixTimestamp.format("yyyy-MM-dd hh:mm:ss");
				}},
				{field:'action',title:'操作',width:100,formatter : function(value, row, index) {
					var str = '';
					if ($.canDelete) {
						str += $.formatString('<img style="cursor:pointer" onclick="delFun(\'{0}\');" src="{1}" title="删除房屋"/>', index, '${path}/images/extjs_icons/delete.png');
					}
					return str;
				}}
			]],
			sortName: 'gmtCreate',
			sortOrder: 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			pagination:true,
			rownumbers:true,
			singleSelect:true, // 是否单选
			onLoadSuccess : function() {
				parent.$.messager.progress('close');
			},
			onRowContextMenu : function(e, index, row){
				e.preventDefault();
				dataGrid.datagrid("unselectAll");
				dataGrid.datagrid("selectRow",index);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
		parent.$.messager.progress('close');
		
	});
	
	function addFun(){
		var r=$("#form").form("validate");
		if(r){
			$.post("/accountDoor/add",$("#form").serializeArray(),function(res){
				showMsg(res);
				if(res.success){
					dataGrid.datagrid("reload");
				}
			},'json');	
		}
	}
	
	function delFun(index){
		if(index) dataGrid.datagrid("selectRow",index);
		var row = dataGrid.datagrid('getSelected');
		if(row){
			var param={'accountId':row.accountId,'doorId':row.doorId};
			$.post("/accountDoor/delete",param,function(res){
				showMsg(res);
				if(res.success){
					dataGrid.datagrid("reload");
				}
			},'json');
		}
	}
	function showMsg(res){
		$("#info_div").show();
		$("#info").text(res.msg);
		if(res.success){
			$("#info").css("color","green");
			$("#info_img").attr("src","${path}/images/extjs_icons/tick.png");
		}else{
			$("#info").css("color","red");
			$("#info_img").attr("src","${path}/images/extjs_icons/exclamation.png");
		}
		setTimeout(function(){
			$("#info_div").hide();
		},3000);
	}
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'north',title:'选择',border:false" style="height: 134px; overflow: hidden;">
		<form id="form">
			<input type="hidden" name="accountId" value="${account.id}" />
			<input type="hidden" name="status" value="1" />
			<table class="table table-hover table-condensed" style="">
				<tr>
					<td>小区</td>
					<td><input id="residenceId" name="residenceId" /></td>
					<td>楼栋</td>
					<td><input id="buildingId" name="buildingId" class="easyui-combobox" panelHeight='auto' /></td>
				</tr>
				<tr>
					<td>单元</td>
					<td><input id="unitId" name="unitId" class="easyui-combobox" panelHeight='auto' /></td>
					<td>门牌</td>
					<td><input id="doorId" name="doorId" class="easyui-combobox" required="true" panelHeight='auto' /></td>
				</tr>
				<tr>
					<td>业主</td>
					<td>
						是:<input type="radio" name='owner' value="1" />
						否:<input type="radio" name='owner' value="0" checked="checked" />
					</td>
					<td>操作</td>
					<td>
						<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'pencil_add'">添加</a>				
					</td>
				</tr>
				<tr>
					<td colspan="4"><div id="info_div" style="display: none;position: absolute;"><img id="info_img" src / ><span id='info'></span></div></td>
				</tr>
			</table>
		</form>
	</div>
	<div data-options="region:'center',border:false" title="" style="overflow: auto;">
		<table id="dataGrid"> </table>
	</div>
</div>